package antempire.control.game.operation.wateroperation;

import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.MathUtils;

import android.util.Log;
import antempire.control.game.GameConstants;
import antempire.control.game.operation.ModelInformationController;

public class WaterSprite extends AnimatedSprite implements GameConstants {

	private static final float BULLET_VELOCITY = 200;

	private PhysicsHandler waterphysic;

	private float Velocity_X, Velocity_Y;

	AnimatedSprite captureWater = null;

	public WaterSprite(float pX, float pY,
			TiledTextureRegion pTiledTextureRegion, final BaseGameActivity activity) {
		super(pX-86, pY-80, pTiledTextureRegion);
		super.setSize(ModelInformationController.getInstance()
				.getWaterInformation().getCaptureWaterInformation()
				.get_size_w(), ModelInformationController.getInstance()
				.getWaterInformation().getCaptureWaterInformation()
				.get_size_w());
		super.animate(100, false, new IAnimationListener() {
			@Override
			public void onAnimationEnd(final AnimatedSprite pAnimatedSprite) {
				activity.runOnUpdateThread(new Runnable() {
					@Override
					public void run() {
						//pAnimatedSprite.clearUpdateHandlers();
						//pAnimatedSprite.clearEntityModifiers();
						pAnimatedSprite.unregisterUpdateHandler(waterphysic);
						activity.getEngine().getScene()
								.getChild(LAYER_CAPTURE_WATER)
								.detachChild(pAnimatedSprite);
					}
				});
			}
		});
		waterphysic = new PhysicsHandler(this);
		this.registerUpdateHandler(waterphysic);

	}

	/*
	 * public void bulletRotation(float deg){ this.setAni(deg); }
	 */
	public void setVelocity() {
		Velocity_X = (float) (BULLET_VELOCITY * Math.sin(MathUtils
				.degToRad(getRotation())));
		Velocity_Y = (float) (BULLET_VELOCITY * Math.cos(MathUtils
				.degToRad(getRotation())));
		Velocity_X = 0;
		Velocity_Y = 0;
		waterphysic.setVelocity(Velocity_X, -Velocity_Y);
	}

	/*
	 * public AnimatedSprite lauchNet(TiledTextureRegion
	 * captureWaterTextureRegionMap, final BaseGameActivity activity, GameSound
	 * sound){
	 */
	public AnimatedSprite lauchCaptureWater(
			TiledTextureRegion captureWaterTextureRegion,
			final BaseGameActivity activity) {
		captureWater = new AnimatedSprite(this.getX() - 86, this.getY() - 80,
				captureWaterTextureRegion.clone());

		captureWater.setSize(ModelInformationController.getInstance()
				.getWaterInformation().getCaptureWaterInformation()
				.get_size_w(), ModelInformationController.getInstance()
				.getWaterInformation().getCaptureWaterInformation()
				.get_size_w());
		captureWater.animate(80, false, new IAnimationListener() {
			@Override
			public void onAnimationEnd(AnimatedSprite pAnimatedSprite) {
				activity.runOnUpdateThread(new Runnable() {
					@Override
					public void run() {
						activity.getEngine().getScene()
								.getChild(LAYER_CAPTURE_WATER)
								.detachChild(captureWater);
					}
				});
			}
		});
		activity.getEngine().getScene().getChild(LAYER_CAPTURE_WATER)
				.attachChild(captureWater);
		// sound.netOpen();
		return captureWater;
	}

	public boolean isOutOfLength() {
		if (this.getY() < ModelInformationController.getInstance()
				.getWaterInformation().getMinWeight()
				|| this.getY() > CAMERA_WIDTH
				|| this.getX() < ModelInformationController.getInstance()
						.getWaterInformation().getMinHeight()
				|| this.getX() > ModelInformationController.getInstance()
						.getWaterInformation().getMaxHeight())
			return true;
		return false;
	}

}
